<?php
/**
 * Created by PhpStorm.
 * User: xp
 * Date: 2020/3/11
 * Time: 9:26
 */

namespace app\controller\test;



use app\controller\CoreController;
use support\Request;

class ViewLogController extends CoreController
{
    protected $indexDirPath = [];
    public function __construct()
    {
    }

    public function index(Request $request)
    {
        if ($request->method() === 'POST') {
            if(config('app.app_env')){
                switch (strtolower(config('app.app_env'))){
                    case 'local':
                        $this->indexDirPath = [
                            runtime_path('logs/request')=>'请求记录',
                            runtime_path('logs/sql')=>'sql语句记录',
                            runtime_path('logs/exception')=>'请求异常',
                            runtime_path('logs/console_exception')=>'脚本异常',
                            runtime_path('logs/console_sql')=>'脚本sql语句',
                            runtime_path('logs/queue_exception')=>'redis队列异常',
                            runtime_path('logs/queue_error')=>'redis队列错误',
                            runtime_path('logs/data')=>'业务日志',

                        ];
                        break;
                    case 'test':
                    case 'prod':
                        $this->indexDirPath = [
                            runtime_path('logs/request')=>'请求记录',
                            runtime_path('logs/sql')=>'sql语句记录',
                            runtime_path('logs/exception')=>'请求异常',
                            runtime_path('logs/console_exception')=>'脚本异常',
                            runtime_path('logs/console_sql')=>'脚本sql语句',
                            runtime_path('logs/queue_exception')=>'redis队列异常',
                            runtime_path('logs/queue_error')=>'redis队列错误',
                            runtime_path('logs/data')=>'业务日志',
                        ];
                        break;
                }
            }

            $colors = [
                'FF7F24','698B22','8B6914','CDCD00','CD3278','848484','8EE5EE','8B6914','FF7F24','8B6914',
                'FF7F24','698B22','8B6914','CDCD00','CD3278','848484','8EE5EE','8B6914','FF7F24','8B6914',
            ];

            $assignPath = [];
            foreach($this->indexDirPath as $path=>$val){
                if(is_dir($path)){
                    $assignPath[] = [
                        'path'=>$path,
                        'name'=>$val,
                    ];
                }
            }

            return $this->endSuccess(['colors'=>$colors,'assign_path'=>$assignPath,'domain'=>trim(config('app.app_url'),'/').'/']);
        }
        return view('viewlog/index');
    }

    public function getView(Request $request)
    {
        $dirpath = $request->input('dirpath');
        if ($request->method() === 'POST') {
            $data = [];
            if($dirpath && is_dir($dirpath)){
                foreach (scandir($dirpath) as $name){
                    $filePath = $dirpath.'/'.$name;
                    if($name=='.' || $name=='..'){
                        continue;
                    }else{
                        if(is_dir($filePath)){
                            $data[] = ['name'=>$name,'url'=>$filePath,'size'=>'','time'=>'','isfile'=>0];
                        }elseif (is_file($filePath)){
                            $data[] =['name'=>$name,'url'=>$filePath,
                                      'size'=>filesize($filePath),'time'=>date('Y-m-d H:i:s',filemtime($filePath)),'isfile'=>1];
                        }

                    }
                }
            }
            return $this->endSuccess(['data'=>$data,'domain'=>trim(config('app.app_url'),'/').'/']);
        }
        if($dirpath && is_file($dirpath)){
            return response(file_get_contents($dirpath),200,['Content-Type' => 'text/plain;charset=utf-8']);
        }else{
            return view('viewlog/getview',['domain'=>trim(config('app.app_url'),'/').'/','dirpath'=>$dirpath]);
        }



    }
}
